package com.example.ass.controller;

import com.example.ass.pojo.Student;
import com.example.ass.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/students")
@Validated
//@CrossOrigin(origins = "http://localhost:8861")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/findByName")
    @CrossOrigin
    public Student findByStudentName(@RequestParam String name) {
        return studentService.findByStudentName(name);
    }

    @PostMapping("/register")
    @CrossOrigin
    public String register(@RequestParam String name, @RequestParam String password) {
        studentService.register(name, password);
        return "Student registered successfully!";
    }

    @PostMapping("/login")
    @CrossOrigin
    public String login(@RequestParam String name, @RequestParam String password) {
        Student student = studentService.login(name, password);
        if (student != null) {
            return "成功 " + student.getName() + "!"; // 使用 getName() 方法
        }
        return "是人类？"; // 登录失败的提示
    }
}